//
//  ProductListTableViewCell.m
//  Castlery
//
//  Created by xie weizhi on 8/8/17.
//  Copyright © 2017 xie weizhi. All rights reserved.
//

#import "ProductListTableViewCell.h"
#import "Product.h"
#import <YYKit.h>

@interface ProductListTableViewCell ()
@property (nonatomic, strong) UIImageView *productImageView;
@property (nonatomic, strong) UILabel *productTitleLabel;
@property (nonatomic, strong) UILabel *productPriceLabel;
@end

@implementation ProductListTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupContentView];
    }
    
    return self;
}

- (void)setupContentView {
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    // image
    self.productImageView = [UIImageView new];
    self.productImageView.contentMode = UIViewContentModeScaleAspectFit;
    self.productImageView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.productImageView];

    [NSLayoutConstraint
     activateConstraints:@[[self.productImageView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor],
                           [self.productImageView.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor],
                           [self.productImageView.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor],
                           [self.productImageView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor
                                                                              constant:-60.f],]];
    
    // title
    self.productTitleLabel = [UILabel new];
    self.productTitleLabel.font = [UIFont boldSystemFontOfSize:14.f];
    self.productTitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.productTitleLabel];
    
    [NSLayoutConstraint
     activateConstraints:@[[self.productTitleLabel.topAnchor constraintEqualToAnchor:self.productImageView.bottomAnchor
                                                                               constant:10.f],
                           [self.productTitleLabel.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor
                                                                             constant:20.f]]];
    
    /// price
    self.productPriceLabel = [UILabel new];
    self.productPriceLabel.font = [UIFont boldSystemFontOfSize:10.f];
    self.productPriceLabel.textAlignment = NSTextAlignmentRight;
    self.productPriceLabel.translatesAutoresizingMaskIntoConstraints = NO;
    [self.contentView addSubview:self.productPriceLabel];
    
    [NSLayoutConstraint
     activateConstraints:@[[self.productPriceLabel.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor
                                                                              constant:-20.f],
                           [self.productPriceLabel.centerYAnchor constraintEqualToAnchor:self.productTitleLabel.centerYAnchor],
                           ]];
    
    
}

#pragma mark - Public

- (void)configureCellWithProduct:(Product *)product {
    self.productTitleLabel.text = product.title;
    self.productPriceLabel.text = product.price;
    
    [self.productImageView setImageWithURL:[product firstImageURL] options:YYWebImageOptionProgressiveBlur];
    
}

@end
